package com.controller;

import com.pojo.Books;
import com.service.BookService;
import com.service.BookServiceImpl;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @author FengTianhao
 * @date 2021/3/11 22:04
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;
    @RequestMapping("/allBook")
    public String list(Model model)
    {

        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list",list);
        return "allBook";
    }

    //
    @RequestMapping("/toAddBook")
    public String toAddPaper(){
        return "addBook";
    }

    //
    @RequestMapping("/addBook")
    public String  addBook(Books books){
        System.out.println("what fuck");
        bookService.addBook(books);
        return "redirect:/book/allBook";//重定向
    }


    @RequestMapping("/toUpdate")
    public String toUpdatePaper(int id,Model model){
        Books books = bookService.queryBookById(id);
        System.out.println("fuckasasdsaklnskskj");
        System.out.println(books);
        model.addAttribute("QBook",books);
        return "updateBook";
    }
    //修改书籍
    @RequestMapping("/updateBook")
    public String updateBook(Books books)
    {
        System.out.println("fuck shoit");
        System.out.println(books);
        bookService.updateBook(books);
        return "redirect:/book/allBook";
    }

    //删除书籍
    @RequestMapping("/deleteBook/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id)
    {
        bookService.deleteBookById(id);

        return "redirect:/book/allBook";
    }

    //查询书籍
    @RequestMapping("/queryBook")
    public String queryBook(String queryBookName, Model model)
    {
        System.out.println(queryBookName);
        Books books = bookService.queryBookByName(queryBookName);
        List<Books> list = new ArrayList<>();
        list.add(books);
        System.out.println(books);
        if(books==null)
        {
            list = bookService.queryAllBook();
            model.addAttribute("erro","未查到");
        }
        model.addAttribute("list",list);

        return "allBook";
    }
}
